clear;close all;clc;
% freqs = [0 100:100:3000];
freqs=25:25:5000;
% nturns=[linspace(0.1, 0.9, 6) linspace(1, 2, 24)];
% rad=0.2;
rad=0.1;
Norder=3;
thre = 0.3;
% onsets=[13.5 13 13.5 12.5 13 12.5 12.5 12.5 13 12 ...
% 12    12 13   12   8 14    12  12  16.5 13]
%
onsets = [14 18.5 12.5 15 12.5 21.5 13 12.5 12 12.5 ...
    12 12.5 12.5 12.5 15 12 12 12 12] % for different locations

period = 10;
fs = 44100;
% whos
plot_each =0;
Num_centers = 1;
Proto_Num = 41;

sides = {'Left','Right'};


% f2=figure('position',[100 70 1600 800]);
for ifreql = 1:length(freqs)
    % for ifreql = length(freqs)
    % for ifreql = 1
    if plot_each==1
        f1=figure('position',[60 70 1600 700]);
    end
    freq = freqs(ifreql)
    tdur = 40/freq;
    for irep = 1:10
        % for irep = length(Angles)
        % cc = 0;
        Angle = 30;
        PString = num2str(Proto_Num);
        if Angle < 0
            ss = '-';
        else
            ss = '+';
        end
        fname= ['C:\Users\gaiy\OneDrive - Saint Louis University\speech_experiments\Analyze_localization_result\3Dio_all\Protocol_41\Protocol_' PString '_deg30_' num2str(irep) '.mp3'];

        [YY fs2]=audioread(fname);
        recorded_sound=resample(YY,441,320);
        left_sound = recorded_sound(:,1);
        right_sound = recorded_sound(:,2);

        % figure;
        % plot(left_sound);hold on;plot(right_sound)
        % title([num2str(freq) ' Hz'])
        % pause(0.2)

        [SL,SR,freq_pos,flag]=mySTFT(left_sound,right_sound,fs,thre,tdur);
        % normalization:
        % [SL1,SR1]=Normalize(SL,SR);
        % if Angle < 0
        %     L_or_R=1;
        % else
        L_or_R=2;
        % end
        SLR=Normalize_Both(SL,SR,L_or_R);
        [Y Ind] = min(abs(freq_pos - freq));
        X_P = [real(SLR(Ind,flag>0))' imag(SLR(Ind,flag>0))'];

        if plot_each==1
            figure(f1)
            subplot(4,5,irep);
            plot(X_P(:,1),X_P(:,2),'k.');hold on;axis square
        end
        % end

        out = f_mycenter(X_P,rad);
        F_centers(irep,:) = out.C1;

        %         if Angle < 0
        %             Pangle =['-' num2str(abs(Angles(irep))) ];
        %         elseif Angle ==0
        %             Pangle =[num2str(abs(Angles(irep))) ];
        %         else
        %             Pangle =['+' num2str(abs(Angles(irep))) ];
        %         end
        if plot_each==1
            s=plot(F_centers(irep,1),F_centers(irep,2),'go');hold on;set(s,'linewidth',2)
            %         grid on
            set(gca,'Xtick',-1:0.5:1)
            set(gca,'Ytick',-1:0.5:1)
            %             title([num2str(freq) 'Hz;' Pangle]);
            axis([-1 1 -1 1])
        end
        pause(0.01)
    end % irept

    sen_centers(ifreql,:)=mean(F_centers)
%     hold on;
%     plot(-1:1,[0,0,0],'k', 'LineWidth', 0.5);
%     plot([0,0,0],-1:1,'k','LineWidth',0.5)
%     set(gca,'fontsize',10)
%     xlabel('Re','fontsize',12)
%     ylabel('Im','fontsize',12)
%     hold on

%     figure(f2)
%     subplot(5,6,ifreql)
%     %     subplot(3,5,ifreql-1)
%     plot(F_centers(:,1),F_centers(:,2),'*')
%     title([num2str(freq) ' Hz'])
%     axis([-1 1 -1 1]);axis square
    pause(0.1)
    %==============================
%     out=f_get_spiral(F_centers,Norder,nturns(ifreql));
%     x_spiral=out.x_spiral;
%     y_spiral=out.y_spiral;
   
end % freq


save sen_alone sen_centers freqs
